package dragon.modules.system.dic.intercept;

import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysDic;
import dragon.core.util.GlobalParameter;
import dragon.core.util.StringUtils;
import dragon.model.bizObject.BizObject;
import dragon.model.intercept.Interceptor;
import dragon.modules.system.dic.dao.DicDaoImpl;
import ejp.Database;
import ejw.ServerInterface;

public class DicInterceptor extends Interceptor {

	private DicDaoImpl dd = new DicDaoImpl();

	@Override
	public int beforeAdd(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		String pid = parameterMap.get("PID");
		String id = parameterMap.get("ID");
		String code = parameterMap.get("CODE");
		String name = parameterMap.get("NAME");
		if (!pid.equals(0)) {
			SysDic sd = dd.getDic(Integer.parseInt(pid), true);
			parameterMap.put("DEPTH", String.valueOf(sd.getDepth() + 1));
			parameterMap.put("IDPATH", sd.getIdpath() + id + "/");
			parameterMap.put("CODEPATH", sd.getCodepath() + code + "/");
			parameterMap.put("NAMEPATH", sd.getNamepath() + name + "/");
		} else {
			parameterMap.put("DEPTH", "1");
			parameterMap.put("IDPATH", "/" + id + "/");
			parameterMap.put("CODEPATH", "/" + code + "/");
			parameterMap.put("NAMEPATH", "/" + name + "/");
		}

		return super.beforeAdd(bizObject, parameterMap, sif, db);
	}

	public int beforeMod(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		String id = parameterMap.get("ID");
		String code = parameterMap.get("CODE");
		String name = parameterMap.get("NAME");
		SysDic sd = dd.getDic(Integer.parseInt(id), true);

		if (StringUtils.isInteger(id)) {
			int ID = Integer.parseInt(id);
			if (!sd.getName().equals(name)) {
				dd.namePathRepair(db, ID, sd.getName(), name);
			}
			if (!sd.getClass().equals(code)) {
				dd.codePathRepair(db, ID, sd.getCode(), code);
			}
			// 删除旧的父节点下的缓存缓存
			GlobalParameter.DicChildren.remove(sd.getPid());
		}
		return super.beforeMod(bizObject, parameterMap, sif, db);
	}

	@Override
	public void afterAdd(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		String pid = parameterMap.get("PID");
		String id = parameterMap.get("ID");
		// 修改父节点的isleaf属性
		DML.executeUpdate(db, "update  " + SysDic.tableName + " set isleaf = '0' where id=" + pid);
		// 缓存数据字典
		GlobalParameter.Dics.put(id, dd.getDic(Integer.parseInt(id), true));
		// 移除父级数据字典缓存数据
		GlobalParameter.DicChildren.remove(pid);

		super.afterAdd(bizObject, parameterMap, sif, db);
	}

	@Override
	public int beforeDel(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		try {
			JSONArray rows = (JSONArray) new JSONParser().parse(parameterMap.get("rows"));
			JSONObject jsonObj = null;
			for (Object obj : rows) {
				jsonObj = (JSONObject) obj;
				String id = jsonObj.get("ID").toString();
				String pid = jsonObj.get("PID").toString();
				// 缓存处理
				GlobalParameter.DicChildren.remove(pid);
				GlobalParameter.DicChildren.remove(id);
				GlobalParameter.Dics.remove(String.valueOf(id));
				// 数据库数据处理-删除子节点
				DML.prepareExecuteUpdate(db, "delete from sys.t_sys_dic where idpath like ?", jsonObj.get("IDPATH") + "/%");
			}
		} catch (ParseException e) {
			throw new ExceptionHandler(e);
		}
		return super.beforeDel(bizObject, parameterMap, sif, db);
	}

}
